package com.gxdf.extendedcontrols.gxdfclient.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import json.org.json.JSONArray;

import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.stream.JsonWriter;



public class FieldType {
	
	String name;
	RenderingType render;
	boolean preloaded;
	TypeDomain typeDomain;
	List<String> posValues;
	

	public FieldType() {
		
		this.name="";
		this.render = RenderingType.TEXTBOX;
		this.preloaded = false;
		this.typeDomain= TypeDomain.LITERAL;
		this.posValues = new ArrayList<String>();
				
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public RenderingType getRender() {
		return this.render;
	}

	public void setRender(RenderingType render) {
		this.render = render;
	}

	public boolean isPreloaded() {
		return this.preloaded;
	}

	public void setPreloaded(boolean preloaded) {
		this.preloaded = preloaded;
	}

	public TypeDomain getTypeDomain() {
		return this.typeDomain;
	}

	public void setTypeDomain(TypeDomain type) {
		this.typeDomain = type;
	}
	
	public String getRenderToString(){
		String r = getRender().toString();
		
		return r;
	}

	public List<String> getPosValues() {
		return posValues;
	}

	public void setPosValues(List<String> posValues) {
		this.posValues = posValues;
	}
	
		
	private String capitalize(String s){	
		s = s.toLowerCase();
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
	
	public void buildJson(JsonWriter writer) throws IOException{
		
		String render = capitalize(getRenderToString());
		if(render.equals("Textbox_list")) render = "Textbox";
		
		writer.name("Name").value(getName());
		writer.name("Rendering").value(render);
		writer.name("PreloadedDomain").value("N");
		writer.name("PossibleValues");
		writer.beginObject();
		writer.name("Value");
		writer.beginArray();
		
		for(String s:posValues){		
			writer.value(s);
		}
		
		writer.endArray();		
		writer.endObject();
		writer.name("TypeDomain").value(capitalize(getTypeDomain().toString()));
		
	} 
	
	
	
	

}
